#include "main.h"

#include <stdint.h>
#include <stdio.h>
// #include <windows.h>
#include "../windows_service/os_ticket/os_ticket.h"
#include "../windows_service/tlog/tlog.h"
#include "PhyBattery_Prot.h"

#define TAG "MAIN"

#define SEND_BUFFER_SIZE 256
static uint8_t send_buffer[SEND_BUFFER_SIZE];

static void functional_test_log(void) { TLOGI(TAG, "functional_test_log\n"); }

static void buffer_dump(uint8_t* buffer, uint16_t size) {
  for (uint16_t i = 0; i < size; i++) {
    printf("0X%x ", buffer[i]);
  }
  printf("\n");
}

static void protocol_enter_firmware_upgrade_test(void) {}
static void protocol_temp_test(void) {
  uint16_t send_size;
  uint8_t  cmd_buffer[9] = {0X3A, 0X16, 0X08, 0X01, 0X00, 0X1F, 0X00, 0X0D, 0X0A};
  TLOGI(TAG, "receive_state:%d\n", PhyBattery_Prot_RecvFrame_Process(cmd_buffer, 9));
  send_size = PhyBattery_Prot_SendFrame_Process(send_buffer, SEND_BUFFER_SIZE);
  if (send_size > 0) buffer_dump(send_buffer, send_size);
}
static void protocol_voltage_test(void) {
  uint16_t send_size;
  uint8_t  cmd_buffer[9] = {0X3A, 0X16, 0X09, 0X01, 0X00, 0X20, 0X00, 0X0D, 0X0A};
  TLOGI(TAG, "receive_state:%d\n", PhyBattery_Prot_RecvFrame_Process(cmd_buffer, 9));
  send_size = PhyBattery_Prot_SendFrame_Process(send_buffer, SEND_BUFFER_SIZE);
  if (send_size > 0) buffer_dump(send_buffer, send_size);
}
static void protocol_current_test(void) {
  uint16_t send_size;
  uint8_t  cmd_buffer[9] = {0X3A, 0X16, 0X0A, 0X01, 0X00, 0X21, 0X00, 0X0D, 0X0A};
  TLOGI(TAG, "receive_state:%d\n", PhyBattery_Prot_RecvFrame_Process(cmd_buffer, 9));
  send_size = PhyBattery_Prot_SendFrame_Process(send_buffer, SEND_BUFFER_SIZE);
  if (send_size > 0) buffer_dump(send_buffer, send_size);
}
static void protocol_soc_test(void) {
  uint16_t send_size;
  uint8_t  cmd_buffer[9] = {0X3A, 0X16, 0X0D, 0X01, 0X00, 0X24, 0X00, 0X0D, 0X0A};
  TLOGI(TAG, "receive_state:%d\n", PhyBattery_Prot_RecvFrame_Process(cmd_buffer, 9));
  send_size = PhyBattery_Prot_SendFrame_Process(send_buffer, SEND_BUFFER_SIZE);
  if (send_size > 0) buffer_dump(send_buffer, send_size);
}
static void protocol_residual_capacity_test(void) {
  uint16_t send_size;
  uint8_t  cmd_buffer[9] = {0X3A, 0X16, 0X0F, 0X01, 0X00, 0X26, 0X00, 0X0D, 0X0A};
  TLOGI(TAG, "receive_state:%d\n", PhyBattery_Prot_RecvFrame_Process(cmd_buffer, 9));
  send_size = PhyBattery_Prot_SendFrame_Process(send_buffer, SEND_BUFFER_SIZE);
  if (send_size > 0) buffer_dump(send_buffer, send_size);
}
static void protocol_maximum_pressure_difference_test(void) {
  uint16_t send_size;
  uint8_t  cmd_buffer[9] = {0X3A, 0X16, 0X14, 0X01, 0X00, 0X2B, 0X00, 0X0D, 0X0A};
  TLOGI(TAG, "receive_state:%d\n", PhyBattery_Prot_RecvFrame_Process(cmd_buffer, 9));
  send_size = PhyBattery_Prot_SendFrame_Process(send_buffer, SEND_BUFFER_SIZE);
  if (send_size > 0) buffer_dump(send_buffer, send_size);
}
static void protocol_cycle_number_test(void) {
  uint16_t send_size;
  uint8_t  cmd_buffer[9] = {0X3A, 0X16, 0X17, 0X01, 0X00, 0X2E, 0X00, 0X0D, 0X0A};
  TLOGI(TAG, "receive_state:%d\n", PhyBattery_Prot_RecvFrame_Process(cmd_buffer, 9));
  send_size = PhyBattery_Prot_SendFrame_Process(send_buffer, SEND_BUFFER_SIZE);
  if (send_size > 0) buffer_dump(send_buffer, send_size);
}
static void protocol_read_the_semi_finished_bar_code_test(void) {
  uint16_t send_size;
  uint8_t  cmd_buffer[9] = {0X3A, 0X16, 0X22, 0X01, 0X00, 0X39, 0X00, 0X0D, 0X0A};
  TLOGI(TAG, "receive_state:%d\n", PhyBattery_Prot_RecvFrame_Process(cmd_buffer, 9));
  send_size = PhyBattery_Prot_SendFrame_Process(send_buffer, SEND_BUFFER_SIZE);
  if (send_size > 0) buffer_dump(send_buffer, send_size);
}
static void protocol_battery_string_status_information_test(void) {
  uint16_t send_size;
  uint8_t  cmd_buffer[9] = {0X3A, 0X16, 0X23, 0X01, 0X00, 0X3A, 0X00, 0X0D, 0X0A};
  TLOGI(TAG, "receive_state:%d\n", PhyBattery_Prot_RecvFrame_Process(cmd_buffer, 9));
  send_size = PhyBattery_Prot_SendFrame_Process(send_buffer, SEND_BUFFER_SIZE);
  if (send_size > 0) buffer_dump(send_buffer, send_size);
}
static void protocol_voltage_cell_1_test(void) {
  uint16_t send_size;
  uint8_t  cmd_buffer[9] = {0X3A, 0X16, 0X24, 0X01, 0X00, 0X3B, 0X00, 0X0D, 0X0A};
  TLOGI(TAG, "receive_state:%d\n", PhyBattery_Prot_RecvFrame_Process(cmd_buffer, 9));
  send_size = PhyBattery_Prot_SendFrame_Process(send_buffer, SEND_BUFFER_SIZE);
  if (send_size > 0) buffer_dump(send_buffer, send_size);
}
static void protocol_Voltage_cell_2_test(void) {
  uint16_t send_size;
  uint8_t  cmd_buffer[9] = {0X3A, 0X16, 0X25, 0X01, 0X00, 0X3C, 0X00, 0X0D, 0X0A};
  TLOGI(TAG, "receive_state:%d\n", PhyBattery_Prot_RecvFrame_Process(cmd_buffer, 9));
  send_size = PhyBattery_Prot_SendFrame_Process(send_buffer, SEND_BUFFER_SIZE);
  if (send_size > 0) buffer_dump(send_buffer, send_size);
}
static void protocol_soh_test(void) {
  uint16_t send_size;
  uint8_t  cmd_buffer[9] = {0X3A, 0X16, 0X0C, 0X01, 0X00, 0X23, 0X00, 0X0D, 0X0A};
  TLOGI(TAG, "receive_state:%d\n", PhyBattery_Prot_RecvFrame_Process(cmd_buffer, 9));
  send_size = PhyBattery_Prot_SendFrame_Process(send_buffer, SEND_BUFFER_SIZE);
  if (send_size > 0) buffer_dump(send_buffer, send_size);
}
static void protocol_current_charging_interval_test(void) {
  uint16_t send_size;
  uint8_t  cmd_buffer[9] = {0X3A, 0X16, 0X47, 0X01, 0X00, 0X5E, 0X00, 0X0D, 0X0A};
  TLOGI(TAG, "receive_state:%d\n", PhyBattery_Prot_RecvFrame_Process(cmd_buffer, 9));
  send_size = PhyBattery_Prot_SendFrame_Process(send_buffer, SEND_BUFFER_SIZE);
  if (send_size > 0) buffer_dump(send_buffer, send_size);
}
static void protocol_read_the_finished_bar_code_test(void) {
  uint16_t send_size;
  uint8_t  cmd_buffer[9] = {0X3A, 0X16, 0X7E, 0X01, 0X00, 0X95, 0X00, 0X0D, 0X0A};
  TLOGI(TAG, "receive_state:%d\n", PhyBattery_Prot_RecvFrame_Process(cmd_buffer, 9));
  send_size = PhyBattery_Prot_SendFrame_Process(send_buffer, SEND_BUFFER_SIZE);
  if (send_size > 0) buffer_dump(send_buffer, send_size);
}
static void protocol_read_the_version_information_and_software_product_number_0X00_test(void) {
  uint16_t send_size;
  uint8_t  cmd_buffer[9] = {0X3A, 0X16, 0X7F, 0X01, 0X00, 0X96, 0X00, 0X0D, 0X0A};
  TLOGI(TAG, "receive_state:%d\n", PhyBattery_Prot_RecvFrame_Process(cmd_buffer, 9));
  send_size = PhyBattery_Prot_SendFrame_Process(send_buffer, SEND_BUFFER_SIZE);
  if (send_size > 0) buffer_dump(send_buffer, send_size);
}
static void protocol_read_the_version_information_and_software_product_number_0X03_test(void) {
  uint16_t send_size;
  uint8_t  cmd_buffer[9] = {0X3A, 0X16, 0X7F, 0X01, 0X03, 0X99, 0X00, 0X0D, 0X0A};
  TLOGI(TAG, "receive_state:%d\n", PhyBattery_Prot_RecvFrame_Process(cmd_buffer, 9));
  send_size = PhyBattery_Prot_SendFrame_Process(send_buffer, SEND_BUFFER_SIZE);
  if (send_size > 0) buffer_dump(send_buffer, send_size);
}
static void protocol_read_the_version_information_and_software_product_number_0X04_test(void) {
  uint16_t send_size;
  uint8_t  cmd_buffer[9] = {0X3A, 0X16, 0X7F, 0X01, 0X04, 0X9A, 0X00, 0X0D, 0X0A};
  TLOGI(TAG, "receive_state:%d\n", PhyBattery_Prot_RecvFrame_Process(cmd_buffer, 9));
  send_size = PhyBattery_Prot_SendFrame_Process(send_buffer, SEND_BUFFER_SIZE);
  if (send_size > 0) buffer_dump(send_buffer, send_size);
}
static void protocol_authentication_discharge_command_test(void) {
  uint16_t send_size;
  uint8_t  cmd_buffer[9] = {0X3A, 0X16, 0XA0, 0X01, 0X01, 0XB8, 0X00, 0X0D, 0X0A};
  TLOGI(TAG, "receive_state:%d\n", PhyBattery_Prot_RecvFrame_Process(cmd_buffer, 9));
  send_size = PhyBattery_Prot_SendFrame_Process(send_buffer, SEND_BUFFER_SIZE);
  if (send_size > 0) buffer_dump(send_buffer, send_size);
}
static void protocol_switching_mos_command_test(void) {
  uint16_t send_size;
  uint8_t  cmd_buffer[11] = {0X3A, 0X16, 0XA6, 0X03, 0X00, 0X00, 0X00, 0XBF, 0X00, 0X0D, 0X0A};
  TLOGI(TAG, "receive_state:%d\n", PhyBattery_Prot_RecvFrame_Process(cmd_buffer, 11));
  send_size = PhyBattery_Prot_SendFrame_Process(send_buffer, SEND_BUFFER_SIZE);
  if (send_size > 0) buffer_dump(send_buffer, send_size);
}
static void protocol_read_mos_command_test(void) {
  uint16_t send_size;
  uint8_t  cmd_buffer[9] = {0X3A, 0X16, 0XA7, 0X01, 0X00, 0XBE, 0X00, 0X0D, 0X0A};
  TLOGI(TAG, "receive_state:%d\n", PhyBattery_Prot_RecvFrame_Process(cmd_buffer, 9));
  send_size = PhyBattery_Prot_SendFrame_Process(send_buffer, SEND_BUFFER_SIZE);
  if (send_size > 0) buffer_dump(send_buffer, send_size);
}

void main() {
  // protocol_enter_firmware_upgrade_test();
  // protocol_temp_test();
  // protocol_voltage_test();
  // protocol_current_test();
  // protocol_soc_test();
  // protocol_residual_capacity_test();
  // protocol_maximum_pressure_difference_test();
  protocol_cycle_number_test();
  // protocol_read_the_semi_finished_bar_code_test();
  // protocol_battery_string_status_information_test();
  // protocol_voltage_cell_1_test();
  // protocol_Voltage_cell_2_test();
  // protocol_soh_test();
  // protocol_current_charging_interval_test();
  // protocol_read_the_finished_bar_code_test();
  // protocol_read_the_version_information_and_software_product_number_0X00_test();
  // protocol_read_the_version_information_and_software_product_number_0X03_test();
  // protocol_read_the_version_information_and_software_product_number_0X04_test();
  // protocol_authentication_discharge_command_test();
  // protocol_switching_mos_command_test();
  // protocol_read_mos_command_test();

  os_create_thread(functional_test_log, 1000);

  os_thread_start();
}